# -*- coding: utf-8 -*-
"""
cesc wallet bot Project

This project was developed by Seye (cescwalletbot@protonmail.com)

Dependencies:
pip pytelegrambotapi
pip pyqrcode
pip pypng
pip pyqrcode
pip certifi
pip urllib3

"""

import subprocess
import cauxiliary


def new(username):
    """command form is getnewaddress [account]"""
    username = str(username)
    check = subprocess.check_output(['./cryptoescudod', 'getaddressesbyaccount', username])
    check = str(check)
    if len(check) < 10:
        address = subprocess.check_output(['./cryptoescudod', 'getnewaddress', username])
        return address
    if check[10] == 'C':
        address = 10
        return address


def send(username, toaddress, amount):
    """command form is sendfrom <account> <toadress> <ammount>.
    
    Returns txid, total amount send, tx fee value
    """
    username = str(username)
    amount = str(amount)
    for z in range(len(amount), 0, -1):
        real_amount = amount[0:z]
        if cauxiliary.is_float(real_amount) is True:
            break
    balance = subprocess.check_output(['./cryptoescudod', 'getbalance', username])
    balance = float(balance)
    check = subprocess.check_output(['./cryptoescudod', 'getaddressesbyaccount', username])
    check = str(check)
    if len(check) < 10:
        return (50, 0, 0)
    if cauxiliary.is_float(amount) is False:
        return (0, 0, 0)
    if cauxiliary.is_float(toaddress) is True:
        return (10, 0, 0)
    if round(float(amount), 8) > balance:
        return (20, 0, 0)
    if round(float(amount), 8) < 0:
        return (30, 0, 0)
    check2 = subprocess.check_output(['./cryptoescudod', 'validateaddress', toaddress])
    if str(check2).find('true') is -1:
        return (40, 0, 0)
    try:
        username = str(username)
        txid = subprocess.check_output(['./cryptoescudod', 'sendfrom', username, toaddress, real_amount])
        transactions = subprocess.check_output(['./cryptoescudod', 'listtransactions', username, '1'])
        transaction = str(transactions)
        for x in range(0, len(transaction), 1):
            amount_check = transaction[x: x + 6]
            fee_check = transaction[x: x + 3]
            if amount_check == 'amount':
                for z in range(len(transaction), 0, -1):
                    s = transaction[x + 10:x + 10 + z]
                    if cauxiliary.is_float(s) is True:
                        sent_amount = float(s)
                        break
                    else:
                        sent_amount = 0
            if fee_check == 'fee':
                for w in range(len(transaction), 0, -1):
                    m = transaction[x + 7:x + 7 + w]
                    if cauxiliary.is_float(m) is True:
                        fee_amount = float(m)
                        break
                    else:
                        fee_amount = 0
        return [txid, sent_amount, fee_amount]
    except subprocess.CalledProcessError:
        return (60, 0, 0)


def balance(username):
    """command form is getbalance [account].
    
    return balance as given by the wallet
    """
    username = str(username)
    check = subprocess.check_output(['./cryptoescudod', 'getaddressesbyaccount', username])
    check = str(check)
    if len(check) < 10:
        balance = -10
        return balance
    if check[10] is 'C':
        balance = subprocess.check_output(['./cryptoescudod', 'getbalance', username])
        return balance


def address(username):
    """comand form is getaccountadress <account>"""
    username = str(username)
    a = subprocess.check_output(['./cryptoescudod', 'getaddressesbyaccount', username])
    a = str(a)
    if len(a) < 10:
        address = 10
        return address
    if a[10] is 'C':
        address = a[10:44]
        return address


def transactions(username):
    """command form is listtransactions [account] [number]"""
    username = str(username)
    try:
        transactions = subprocess.check_output(['./cryptoescudod', 'listtransactions', username, '5'])
        transactions = str(transactions)
        return transactions
    except subprocess.CalledProcessError:
        return '10'